<?php
/* $Id: gmap_views.module,v 1.1.2.8 2007/04/27 20:42:29 bdragon Exp $ */
// vim:set ft=php:

/**
 * @file
 * GMap Views: A Views Style plugin providing a GMap view.
 */

/**
 * Implementation of hook_views_style_plugins().
 */
function gmap_views_views_style_plugins() {
  return array(
    'gmap' => array(
      'name' => t('Gmap View'),
      'theme' => 'views_view_gmap',
      'needs_fields' => true,
      'validate' => 'gmap_views_validate',
    )
  );
}

/**
 * Validate a GMap View.
 * GMap Views requires Location: Latitude and Location: Longitude
 * to generate markers.
 */
function gmap_views_validate($type, $view, $form) {
  $lat_set = FALSE;
  $lon_set = FALSE;
  for ($n = 0 ; $n < $view['field']['count'] ; $n++) {
    if ($view['field'][$n]['id'] == 'location.latitude') {
      $lat_set = TRUE;
    }
    if ($view['field'][$n]['id'] == 'location.longitude') {
      $lon_set = TRUE;
    }
  }
  if (!($lat_set && $lon_set)) {
    form_error($form["$type-info"][$type . '_type'], t('GMap View requires Location: Latitude and Location: Longitude!'));
  }
  return views_ui_plugin_validate_list($type, $view, $form);
}

/**
 * Display the results of a view in a Google Map.
 */
function theme_views_view_gmap($view, $results) {
  // Fields are used to render the markers.
  $fields = _views_get_fields();

  if (isset($view->gmap_macro) && $view->gmap_macro) {
    $thismap = array(
      '#map' => 'view_gmap',
      '#settings' => array_merge(gmap_defaults(),gmap_parse_macro($view->gmap_macro)),
    );
    if ($thismap['#settings']['behavior']['views_autocenter']) {
      // Find the first valid location.
      foreach ($results as $entry) {
        $location = array(
          'lat' => $entry->location_latitude,
          'lon' => $entry->location_longitude,
        );
        if (($location['lat']) && ($location['lon'])) {
          // Set default location for map
          $thismap['#settings']['latitude'] = $location['lat'];
          $thismap['#settings']['longitude'] = $location['lon'];
          // Break loop because we have what we want.
          break;
        }
      }
    }

  }
  else {
    $thismap = array(
      '#map' => 'view_gmap',
      '#settings' => gmap_defaults(),
    );
  }

  $fatmarkers = (isset($thismap['#settings']['behavior']['fatmarkers']) && $thismap['#settings']['behavior']['fatmarkers']);

  $markers = array();

  if ($fatmarkers) {
    $thismap['#settings']['viewfields'] = $view->field;
    $datafields = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        $datafields[] = $field['queryname'];
      }
    }
  }

  foreach ($results as $entry) {
    // @@@
    $type = isset($entry->node_type) ? $entry->node_type : '';
    $location = array(
      'lat' => $entry->location_latitude,
      'lon' => $entry->location_longitude,
    );
    if (($location['lat']) && ($location['lon'])) {
      if ($fatmarkers) {
        $data = array();
        foreach ($view->field as $field) {
          if ($fields[$field['id']]['visible'] !== FALSE) {
            $data[] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $entry, $view);
          }
        }
        $themarker = array(
          'markername' => variable_get('gmap_node_marker_'.$type, ''),
          'latitude' => $location['lat'],
          'longitude' => $location['lon'],
          'view' => array_values($data),
        );
      }
      else {
        $marker_label = theme('gmap_views_marker_label', $view, $fields, $entry);

        $themarker = array(
          // @@@
          'markername' => variable_get('gmap_node_marker_'.$type, ''),
          'text' => strtr($marker_label,"'\n\r",'" '),
          'latitude' => $location['lat'],
          'longitude' => $location['lon']
        );
      }

      $markers[] = $themarker;
    }
  }
  $thismap['#settings']['markers'] = $markers;
  $output .= theme('gmap',$thismap);
  return $output;
}

/**
 * Theme a marker label.
 */
function theme_gmap_views_marker_label($view, $fields, $entry) {
  $marker_label = '';
  foreach ($view->field as $field) {
    $marker_label .= '<div class="'. $field['field'] .'">'. views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $entry, $view) .'</div>';
  }
  return $marker_label;
}